package service

import (
	"errors"
	"mall-portalv2/conf"
	pd "mall-portalv2/internal/dao/control"
	"time"
)

func (s *Service) CommonGrpcRequest(done chan interface{}, handlerFunc func()) (*pd.Result, error) {

	go handlerFunc()

	select {
	case res := <-done:
		_, ok := res.(error)
		if ok {
			return nil, res.(error)
		} else {
			return res.(*pd.Result), nil
		}
	case <-time.After(time.Duration(conf.Config.Grpc.Timeout * time.Millisecond)):
		return nil, errors.New("Grpc request timeout")
	}
}
